/* (c)2010-2012 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.controller;

import javax.portlet.ActionResponse;

import org.ajm.spring.survey.modules.admin.model.Question;
import org.ajm.spring.survey.modules.admin.service.AnswerService;
import org.ajm.spring.survey.modules.admin.service.QuestionService;
import org.ajm.spring.survey.modules.admin.service.SurveyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.PortletRequestDataBinder;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

/**
 * The controller for the list questions and delete question operations on the Question object.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
@RequestMapping("VIEW")
@Controller(value="questionController")
public class QuestionController {

    // ------------------------------------------------------------------------|
    // --- CONSTANTS ----------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // --- CLASS VARIABLES ----------------------------------------------------|
    // ------------------------------------------------------------------------|
    // ------------------------------------------------------------------------|

    static Logger logger = Logger.getLogger(QuestionController.class);
    
    // ------------------------------------------------------------------------|
    // --- INSTANCE VARIABLES -------------------------------------------------|
    // ------------------------------------------------------------------------|

    @Autowired
    @Qualifier("surveyService")
    private SurveyService surveyService;
    
    @Autowired
    @Qualifier("questionService")
    private QuestionService questionService;
    
    @Autowired
    @Qualifier("answerService")
    private AnswerService answerService;
    
    // ------------------------------------------------------------------------|
    // --- CONSTRUCTORS -------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- CLASS METHODS ------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE METHODS ---------------------------------------------------|
    // ------------------------------------------------------------------------|

    /**
     * Setup binder for adding/editing surveys.
     */
    @InitBinder
    protected void initBinder(PortletRequestDataBinder binder) {
        
        //binder.setAllowedFields(new String[] { "description", "survey", "position" });
    }

    // ------------------------------------------------------------------------|

    /**
     * The action for viewing the questions of a survey.
     * 
     * @param id 
     * @param model 
     * @return The attribute in the model defining the action of viewing the 
     * details of a survey.
     */
    @RenderMapping(params="action=viewSurveyQuestions")
    public String viewSurveyQuestions(@RequestParam("surveyID") Long id, Model model) {
        
        model.addAttribute("survey", getSurveyService().findById(id));

        return "surveyQuestionsView";
    }
    
    // ------------------------------------------------------------------------|

    /**
     * The action for deleting a question.
     * <p>When deleting a question the reference to the question in an answer's
     * skip-to-question field will be removed beforehand if present.</p>
     * 
     * TODO check that all answers for this question are deleted too.
     * 
     * @param response 
     * @param questionId 
     */
    @ActionMapping(params="action=deleteQuestion")
    @Transactional
    public void deleteQuestion(ActionResponse response, @RequestParam("questionID") Long questionId) {
        
        Question question = getQuestionService().findById(questionId);
        getQuestionService().delete(question);
        
        response.setRenderParameter("action", "viewSurveyQuestions");
        response.setRenderParameter("surveyID", question.getSurvey().getId().toString());
    }
    
    
    // --- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --|
    // ---[  GETTERS AND SETTERS  ]--------------------------------------------|
    // --- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --|

    /**
     * @return the surveyService
     */
    public SurveyService getSurveyService() {
        
        return surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param surveyService the surveyService to set
     */
    public void setSurveyService(SurveyService surveyService) {
        
        this.surveyService = surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the questionService
     */
    public QuestionService getQuestionService() {
        
        return questionService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param questionService the questionService to set
     */
    public void setQuestionService(QuestionService questionService) {
        
        this.questionService = questionService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the answerService
     */
    public AnswerService getAnswerService() {
        return answerService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param answerService the answerService to set
     */
    public void setAnswerService(AnswerService answerService) {
        this.answerService = answerService;
    }

    // ------------------------------------------------------------------------|

}
